package com.easybuilder.core.authorization.controller;

import com.alibaba.fastjson.JSONObject;
import com.easybuilder.common.base.controller.BaseController;
import com.easybuilder.common.utils.Base64Util;
import com.easybuilder.common.utils.StringUtil;
import com.easybuilder.core.authorization.service.LoginService;
import com.easybuilder.core.rbac.entity.SysUser;
import com.mysql.cj.util.Base64Decoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Base64;

@Slf4j
@RestController
@RequestMapping("/login")
public class LoginController extends BaseController {
    @Resource
    private LoginService loginService;

    @PostMapping("/doLogin")
    public Result<Object> login(@RequestParam(required = true) String username, @RequestParam(required = true) String password) {
        // TODO: 后续需要对用户名密码传参过程中加密,可通过URLDecord
        log.info("用户登录传参，用户名：{}，密码：{}", Base64Util.decode(username), Base64Util.decode(password));
        SysUser user = loginService.login(Base64Util.decode(username), Base64Util.decode(password));
        if (user == null) {
            return Error(HttpStatus.BAD_REQUEST,"用户名或密码错误");
        }
        return OK(user, "登陆成功");
    }

    /** 退出登录 */
    @PostMapping("/logout")
    public Result<Object> logout(HttpServletRequest request){
        String token = request.getHeader("Authorization");
        if (token == null) {
            return OK("退出登录成功");
        }
        boolean logout = loginService.logout(token);
        return OK("退出登录成功");
    }

    /** 检查登陆状态 */
    @PostMapping("/check")
    public Result<Object> check(HttpServletRequest request) {
        String token = request.getHeader("Authorization");
        if (token == null) {
            return OK(false);
        }
        boolean isLogin = loginService.check(token);
        return OK(isLogin);
    }

    @PostMapping("/regist")
    public Result<Object> regist(@RequestBody SysUser sysUser) {
        log.info("注册接口传参：{}", JSONObject.toJSONString(sysUser));
        if (sysUser == null || StringUtil.isEmpty(sysUser.getUsername()) || StringUtil.isEmpty(sysUser.getPassword())) {
            return Error("注册失败");
        }
        // 解析sysUser
        resolveData(sysUser);
        log.info("解析注册接口传参：{}", JSONObject.toJSONString(sysUser));
        // 注册
        JSONObject result = loginService.regist(sysUser);
        if (result.getBoolean("success")) {
            return OK("注册成功");
        } else {
            return Error(result.getString("message"));
        }
    }

    /** 解析Base64传参 */
    private void resolveData(SysUser sysUser) {
        sysUser.setUsername(Base64Util.decode(sysUser.getUsername()));
        //sysUser.setNickname(Base64Util.decode(sysUser.getNickname()));
        sysUser.setPassword(Base64Util.decode(sysUser.getPassword()));
    }

    public static void main(String[] args) {
        byte[] decode = Base64.getDecoder().decode("YWFh");
        System.out.println(new String(decode));
    }
}
